//
//  YQLoginViewController.swift
//  yq微信
//
//  Created by yq on 2017/8/24.
//  Copyright © 2017年 yq. All rights reserved.
//

import UIKit

class YQLoginViewController: UIViewController {
    //MARK:用户名和密码:属性
  
    //用户名
    @IBOutlet weak var accountName: UITextField!
    //密码
    @IBOutlet weak var password: UITextField!
    
    //MARK:系统回调:函数
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    
    
    /*注意: 此处是开放注册*/
    
    //MARK: 点击注册:按钮
    @IBAction func Register() {
        EMClient.shared().register(withUsername: self.accountName.text, password: self.password.text) { (string, error ) in
            
            print(string!)
            
            if(error==nil){
                print("注册成功")
                //第三方框架：弹框提示
                JDStatusBarNotification.show(withStatus: "注册成功", dismissAfter: 2.0)
            }
        }
    }

    //MARK: 点击登录:按钮
    @IBAction func Login() {
        SVProgressHUD.show(withStatus: "正在登录...")
        // 登录
        EMClient.shared().login(withUsername: self.accountName.text, password: self.password.text) { (string , error) in
            if(error==nil){
                // 1.记录退出的用户名(为了用户在重新登录的时候,不必再次输入用户名, optional)
                //记录用户名
        UserDefaults.standard.set(self.accountName.text, forKey: "accountName")
          //登录成功之后的提醒
                
        SVProgressHUD.showSuccess(withStatus: "登录成功!")
        SVProgressHUD.setDefaultStyle(SVProgressHUDStyle.dark)
        JDStatusBarNotification.show(withStatus: "欢迎回来!", dismissAfter: 2.6)
                
        // 开启自动登录 , 然后在APPdelegate中进行设置自动登录
        // 开启自动登录(OC和Swift混编)
        AutoLogin.setAutoLogin()
        // 获取storyboard
        let tabbarVC = YQTabBarController.yq_tabBarController()
        // 设置窗口根控制器
          UIApplication.shared.keyWindow?.rootViewController  = tabbarVC
            //去除蒙版
        SVProgressHUD.dismiss()
            }else{
                JDStatusBarNotification.show(withStatus: "登录失败!", dismissAfter: 2.0, styleName: JDStatusBarStyleError)
                
                SVProgressHUD.showError(withStatus: "用户名或密码错误!")
                SVProgressHUD.setDefaultStyle(SVProgressHUDStyle.dark)
            }
        }
    }
    }

//MARK:- 快速获取登录界面storyboard
extension YQLoginViewController {
    
    class func getLoginVC()->UIViewController{
        
      
        let sb = UIStoryboard.init(name: "Main", bundle: Bundle.main)
        let vc = sb.instantiateViewController(withIdentifier: "YQLoginViewController")
        return vc
    }

}

